<?php
/**
 * @see Alinks_Form_Element_Interface
 */
require_once('Alinks/Form/Element/Interface.php');

/**
 * Abstract class for the form element classes
 *
 * Copyright (C) 2008  Sean Hickey
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Sean Hickey <sean@headzoo.com>
 * @version $Id: Abstract.php 378 2008-07-03 05:19:31Z sean $
 * @package Alinks_Form
 */
abstract class Alinks_Form_Element_Abstract implements Alinks_Form_Element_Interface
{
	/**
	 * The element name
	 *
	 * @var string
	 */
	protected $_name;
	
	/**
	 * The element id
	 *
	 * @var string
	 */
	protected $_id;
	
	/**
	 * The input value
	 *
	 * @var string
	 */
	protected $_value;
	
	/**
	 * The label for the input element
	 *
	 * @var string
	 */
	protected $_label;
	
	/**
	 * The element description
	 *
	 * @var string
	 */
	protected $_description;
	
	/**
	 * Input element arguments
	 *
	 * @var array
	 */
	protected $_args = array();
	
	/**
	 * Is this form element required?
	 *
	 * @var bool
	 */
	protected $_required = false;
	
	/**
	 * Message to display if the validation fails
	 *
	 * @var string
	 */
	protected $_required_msg = 'This value is required';
	
	/**
	 * A regular expression that the value must match
	 *
	 * @var string
	 */
	protected $_must_match = null;
	
	/**
	 * Message to display if the validation fails
	 *
	 * @var string
	 */
	protected $_must_match_msg = 'This value failed validation';
	
	/**
	 * A regular expression that the value can't match
	 *
	 * @var string
	 */
	protected $_not_match = null;
	
	/**
	 * Message to display if the validation fails
	 *
	 * @var string
	 */
	protected $_not_match_msg = 'This valud failed validation';
	
	/**
	 * Invalid messages
	 *
	 * @var array
	 */
	protected $_messages;
	
	/**
	 * Whether the element is valid or not
	 *
	 * @var bool
	 */
	protected $_valid = true;
	
	/**
	 * Sets the name of the element
	 *
	 * @param string $name The element name
	 * @return Alinks_Form_Element_Abstract
	 */
	public function setName($name)
	{
		$this->_name = $name;
	}
	
	/**
	 * Returns the name of the element
	 *
	 * @return string
	 */
	public function getName()
	{
		return $this->_name;
	}
	
	/**
	 * Sets the element id
	 *
	 * @param string $id The element id
	 * @return Alinks_Form_Element_Abstract
	 */
	public function setId($id)
	{
		$this->_id = $id;
		return $this;
	}
	
	/**
	 * Returns the element id
	 *
	 * @return string
	 */
	public function getid()
	{
		if (null === $this->_id) {
			$this->_id = $this->_name;
		}
		return $this->_id;
	}
	
	/**
	 * Sets the input value
	 *
	 * @param string $value The value
	 * @return Alinks_Form_Element_Abstract
	 */
	public function setValue($value)
	{
		$previous = $this->_value;
		$this->_value = $value;
		$this->_validate($value);
		if (!$this->_valid) {
			$this->_value = $previous;
		}
		
		return $this;
	}
	
	/**
	 * Sets the label for the input element
	 *
	 * @param string $label The label
	 * @return Alinks_Form_Element_Abstract
	 */
	public function setLabel($label)
	{
		$this->_label = $label;
		return $this;
	}
	
	/**
	 * Sets the element description
	 *
	 * @param string $description The description
	 * @return Alinks_Form_Element_Abstract
	 */
	public function setDescription($description)
	{
		$this->_description = $description;
		return $this;
	}
	
	/**
	 * Sets the element arguments
	 *
	 * @param array $args The arguments
	 * @return Alinks_Form_Element_Abstract
	 */
	public function setArgs(array $args)
	{
		$this->_args = array();
		foreach($args as $key => $value) {
			$this->addArg($key, $value);
		}
		return $this;
	}
	
	/**
	 * Returns the input value
	 *
	 * @return string
	 */
	public function getValue()
	{
		return $this->_value;
	}
	
	/**
	 * Returns the label for the input element
	 *
	 * @return string
	 */
	public function getLabel()
	{
		$label = htmlspecialchars($this->_label);
		if ($this->isRequired()) {
			$label .= ' <em>*</em>';
		}
		return sprintf('<label for="%s">%s</label>', $this->getName(), $label);
	}
	
	/**
	 * Returns the label without any HTML formatting
	 *
	 * @return string
	 */
	public function getRawLabel()
	{
		return $this->_label;
	}
	
	/**
	 * Returns the element description
	 *
	 * @return string
	 */
	public function getDescription()
	{
		return $this->_description;
	}
	
	/**
	 * Adds a single element argument
	 *
	 * @param string $key The argument key
	 * @param string $value The argument value
	 * @return Alinks_Form_Element_Abstract
	 */
	public function addArg($key, $value)
	{
		$this->_args[$key] = $value;
		return $this;
	}
	
	/**
	 * Returns the set arguments
	 *
	 * @return array
	 */
	public function getArgs()
	{
		return $this->_args;
	}
	
	/**
	 * Returns the HTML for the form element
	 *
	 * @return string
	 */
	public function __toString()
	{
		return $this->getHtml();
	}
	
	/**
	 * Sets if the form element is required
	 *
	 * @param bool $value Required or not
	 * @param string $message The message to display if fails validation
	 * @return Alinks_Form_Element_Abstract
	 */
	public function setRequired($value, $message = 'This element is required')
	{
		$this->_required = (boolean)$value;
		$this->_require_msg = $message;
		return $this;
	}
	
	/**
	 * Returns if the form element is required
	 *
	 * @return bool
	 */
	public function isRequired()
	{
		return $this->_required;
	}
	
	/**
	 * Returns the message to display if a required field is empty
	 *
	 * @return string
	 */
	public function getRequiredMessage()
	{
		return $this->_required_msg;
	}
	
	/**
	 * Sets a regular expression that the value must match to be valid
	 *
	 * @param string $match The must match regexp
	 * @param string $message The message to display if fails validation
	 * @return Alinks_Form_Element_Abstract
	 */
	public function setMustMatch($match, $message = '')
	{
		$this->_must_match = $match;
		$this->_must_match_msg = $message;
		return $this;
	}
	
	/**
	 * Returns the the must match regular expression
	 *
	 * @return string
	 */
	public function getMustMatch()
	{
		return $this->_must_match;
	}
	
	/**
	 * Returns the message to display if the must match field fails
	 *
	 * @return string
	 */
	public function getMustMatchMessage()
	{
		return $this->_must_match_msg;
	}
	
	/**
	 * Sets a regular expression that the value must not match to be valid
	 *
	 * @param string $match The not match regexp
	 * @param string $message The message to display if fails validation
	 * @return Alinks_Form_Element_Abstract
	 */
	public function setNotMatch($match, $message = '')
	{
		$this->_not_match = $match;
		$this->_not_match_msg = $message;
		return $this;
	}
	
	/**
	 * Returns the not match regular expression
	 *
	 * @return string
	 */
	public function getNotMatch()
	{
		return $this->_not_match;
	}
	
	/**
	 * Returns the message to display if a the not match field fails
	 *
	 * @return string
	 */
	public function getNotMatchMessage()
	{
		return $this->_not_match_msg;
	}
	
	/**
	 * Returns if the set form values are valid
	 *
	 * @return bool
	 */
	public function isValid()
	{
		return $this->_valid;
	}
	
	/**
	 * Returns any invalid messages
	 *
	 * @return array
	 */
	public function getMessages()
	{
		return $this->_messages;
	}
	
	/**
	 * Converts the array of arguments to a string
	 *
	 * @return string
	 */
	protected function _argsToString()
	{
		$string = null;
		if (!empty($this->_args)) {
			$a = array();
			foreach($this->_args as $key => $value) {
				$value = htmlspecialchars($value);
				$a[] = "$key=\"$value\"";
			}
			$string = join(' ', $a);
		}
		
		return $string;
	}
	
	/**
	 * Validates an input value
	 *
	 * @param string $value The value
	 * @return bool
	 */
	protected function _validate($value)
	{
		if ($this->_required && empty($value)) {
			$this->_valid = false;
			$this->_messages[] = $this->_required_msg;
		}
		if (null !== $this->_must_match && !preg_match('/' . $this->_must_match . '/', $value)) {
			$this->_valid = false;
			$this->_messages[] = $this->_must_match_msg;
		}
		if (null !== $this->_not_match && preg_match('/' . $this->_not_match . '/', $value)) {
			$this->_valid = false;
			$this->_messages[] = $this->_not_match_msg;
		}

		return $this->_valid;
	}
}